package xyz.jangle.msg;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

public class BufferQueue {
	
	private Queue<String> buffer;
	
	private int max = 10;
	
	public BufferQueue() {
		super();
		this.buffer = new LinkedList<String>();
	}

	public synchronized String get() {
		while(buffer.size() == 0) {
			try {
				System.out.println("队列空，进入等待");
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String poll = buffer.poll();
		notify();
		return poll;
	}
	
	public synchronized void set(String str) {
		while(buffer.size() == max) {
			try {
				System.out.println("队列满，进入等待");
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		buffer.add(str);
		notify();
	}

}
